#include<bits/stdc++.h>
using namespace std;
inline int read(){
	char c;int a=0;
	while(!isdigit(c=getchar()));
	do{a=(a<<1)+(a<<3)+(c^48);}while(isdigit(c=getchar()));
	return a;
}
int A[200010],n;
stack<int>fA[2][1000010];
long long DFS(int i,long long val){
	if(i==n)return val;
	bool f[2];
	f[0]=!fA[0][A[i]].empty();
	f[1]=!fA[1][A[i]].empty();
	fA[0][A[i]].push(i);
	long long res=DFS(i+1,val+(f[0]?A[i]:0));
	fA[0][A[i]].pop();
	fA[1][A[i]].push(i);
	res=max(res,DFS(i+1,val+(f[1]?A[i]:0)));
	fA[1][A[i]].pop();
	return res;
}
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int t=read();
	while(t--){
		n=read();
		for(int i=0;i<n;i++){
			A[i]=read();
			while(fA[0][A[i]].size())fA[0][A[i]].pop();
			while(fA[1][A[i]].size())fA[1][A[i]].pop();
		}
		printf("%lld\n",DFS(0,0));
	}
	return 0;
}
